$(document).ready(function() {

    //**** START REFRESH ****//
    $('#layout-refresh-toggle').click(function() {
        location.reload();
    });
    //**** END REFRESH  ****//

    //**** END LAYOUT ADJUST  ****// 
    $('#layout-condensed-toggle').click(function() {
        if ($('#main-menu').attr('data-inner-menu') == '1') {
            //Do nothing
            console.log("Menu is already condensed");
        } else {
            if ($('#main-menu').hasClass('hide')) {
                $('#main-menu').removeClass('hide');
                $('.page-content').removeClass('condensed');
                $('.scrollup').removeClass('to-edge');
                $('.header-seperation').show();
                //Bug fix - In high resolution screen it leaves a white margin
                $('.header-seperation').css('height', '61px');
                $('.footer-widget').show();
            } else {
                $('#main-menu').addClass('hide');
                $('.page-content').addClass('condensed');
                $('.scrollup').addClass('to-edge');
                $('.header-seperation').hide();
                $('.footer-widget').hide();
            }
        }
    });
    //**** END LAYOUT ADJUST  ****//

    //**** BEGIN MENU  ****//
    var handleSidenarAndContentHeight = function() {

        var content = $('.page-content');
        var sidebar = $('.page-sidebar');
        
        if (!content.attr("data-height")) {
            content.attr("data-height", content.height());
        }

        if (sidebar.height() > content.height()) {
            content.css("min-height", sidebar.height() + 120);
        } else {
            content.css("min-height", content.attr("data-height"));
        }
    };

    $('.page-sidebar li > a').on('click', function(e) {
        if ($(this).next().hasClass('sub-menu') == false) {
            return;
        }
        var parent = $(this).parent().parent();

        parent.children('li.open').children('a').children('.arrow').removeClass('open');
        parent.children('li.open').children('.sub-menu').slideUp(200);
        parent.children('li.open').removeClass('open');

        var sub = jQuery(this).next();
        if (sub.is(":visible")) {
            jQuery('.arrow', jQuery(this)).removeClass("open");
            jQuery(this).parent().removeClass("open");
            sub.slideUp(200, function() {
                handleSidenarAndContentHeight();
            });
        } else {
            jQuery('.arrow', jQuery(this)).addClass("open");
            jQuery(this).parent().addClass("open");
            sub.slideDown(200, function() {
                handleSidenarAndContentHeight();
            });
        }

        e.preventDefault();
    });
    //**** END MENU  ****//

    //**** START TOOLS  ****// 
    $('.grid .tools .collapse, .grid .tools .expand').on('click', function() {
        var el = jQuery(this).parents(".grid").children(".grid-body");
        if (jQuery(this).hasClass("collapse")) {
            jQuery(this).removeClass("collapse").addClass("expand");
            el.slideUp(200);
        } else {
            jQuery(this).removeClass("expand").addClass("collapse");
            el.slideDown(200);
        }
    });

    $('.grid .tools a.remove').on('click', function() {
        var removable = jQuery(this).parents(".grid");
        if (removable.next().hasClass('grid') || removable.prev().hasClass('grid')) {
            jQuery(this).parents(".grid").remove();
        } else {
            jQuery(this).parents(".grid").parent().remove();
        }
    });

    $('.controller .remove').click(function() {
        $(this).parent().parent().parent().parent().addClass('animated fadeOut');
        $(this).parent().parent().parent().parent().attr('id', 'id_remove_temp_id');
        setTimeout(function() {
            $('#id_remove_temp_id').remove();
        }, 1);
    });
    //**** END TOOLS  ****// 

    //**** START SET PAGESIZE  ****// 
    $('select.pagesize').change(function() {
        var uri = new Uri(window.location.href)
            .deleteQueryParam('page')
            .deleteQueryParam('size')
            .addQueryParam('size', $.T.to_int($(this).val()))
            .toString();

        $.T.redirect(uri);
    })
    //**** END SET PAGESIZE  ****// 
});